
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Java 基础 -- 关于自动装箱和拆箱机制 · 寒江蓑笠的博客</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="介绍 Java 的自动装箱和拆箱机制">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="java-basic-knowledge-parameter-passing.html" />
    
    
    <link rel="prev" href="java-basic-knowledge-generic.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="java-basic-joor.html">
            
                <a href="java-basic-joor.html">
            
                    
                    Java 基础 -- 使用 JOOR 实现反射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="java-basic-annotation.html">
            
                <a href="java-basic-annotation.html">
            
                    
                    Java基础 -- 注解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="java-basic-threadlocal.html">
            
                <a href="java-basic-threadlocal.html">
            
                    
                    Java 基础 -- ThreadLocal
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="java-development-skills-get-stacktrace.html">
            
                <a href="java-development-skills-get-stacktrace.html">
            
                    
                    Java 开发技巧 -- 获取 Java 的调用栈
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5" data-path="java-basic-concurrent-map-collection.html">
            
                <a href="java-basic-concurrent-map-collection.html">
            
                    
                    Java 基础 -- 并发集合的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.6" data-path="java-dynamic-proxy-invocationhandler-demo.html">
            
                <a href="java-dynamic-proxy-invocationhandler-demo.html">
            
                    
                    Java InvocationHandler 实现动态代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.7" data-path="java-basic-try-catch-finally.html">
            
                <a href="java-basic-try-catch-finally.html">
            
                    
                    Java 基础 -- 深入理解 try catch finally
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8" data-path="java-basic-reflect.html">
            
                <a href="java-basic-reflect.html">
            
                    
                    Java 基础 -- 反射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.9" data-path="java-basic-io-stream.html">
            
                <a href="java-basic-io-stream.html">
            
                    
                    Java IO -- IO流的概念和分类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.10" data-path="java-basic-final.html">
            
                <a href="java-basic-final.html">
            
                    
                    Java 基础 -- 深入理解 final 修饰符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.11" data-path="java-basic-memory-manage-constructor.html">
            
                <a href="java-basic-memory-manage-constructor.html">
            
                    
                    Java 内存管理 -- 父子实例的内存控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.12" data-path="java-basic-memory-manage-inherit-object.html">
            
                <a href="java-basic-memory-manage-inherit-object.html">
            
                    
                    Java 内存管理 -- 构造方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.13" data-path="java-basic-memory-manage-memory-area.html">
            
                <a href="java-basic-memory-manage-memory-area.html">
            
                    
                    Java 内存管理 -- 内存区域的划分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.14" data-path="java-basic-memory-manage-variable.html">
            
                <a href="java-basic-memory-manage-variable.html">
            
                    
                    Java 内存管理 -- 变量的内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.15" data-path="java-basic-knowledge-generic.html">
            
                <a href="java-basic-knowledge-generic.html">
            
                    
                    Java 基础 -- 范型
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.1.16" data-path="java-basic-knowledge-auto-boxing.html">
            
                <a href="java-basic-knowledge-auto-boxing.html">
            
                    
                    Java 基础 -- 关于自动装箱和拆箱机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.17" data-path="java-basic-knowledge-parameter-passing.html">
            
                <a href="java-basic-knowledge-parameter-passing.html">
            
                    
                    Java 基础 -- 值传递、引用传递和参数传递
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.18" data-path="java-basic-stringbuilder-stringbuffer.html">
            
                <a href="java-basic-stringbuilder-stringbuffer.html">
            
                    
                    Java 基础 -- StringBuilder 和 StringBuffer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.19" data-path="java-basic-knowledge-about-string.html">
            
                <a href="java-basic-knowledge-about-string.html">
            
                    
                    Java 基础 -- 深入理解 String
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.20" data-path="java-basic-linkedhashmap.html">
            
                <a href="java-basic-linkedhashmap.html">
            
                    
                    Java 基础 -- LinkedHashMap 的使用和原理
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Java 基础 -- 关于自动装箱和拆箱机制</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x5148;&#x770B;&#x4E00;&#x6BB5;&#x4EE3;&#x7801;">&#x5148;&#x770B;&#x4E00;&#x6BB5;&#x4EE3;&#x7801;</h2>
<pre><code>        Integer f1 = 127, f2 = 127, f3 = 128, f4 = 128;
        Integer f5 = new Integer(10);
        Integer f6 = new Integer(10);

        int i1 = f1, i2 = f2, i3 = f3, i4 = f4;

        System.out.println(f1 == f2);
        System.out.println(f3 == f4);
        System.out.println(f5 == f6);
        System.out.println(i1 == i2);
        System.out.println(i3 == i4);

        int i5 = 10;
        Log.e(&quot;Test&quot;,&quot;&quot;+ (i5 == f6));
</code></pre><p>&#x7ED3;&#x679C;&#x662F;&#xFF1A;true false false true true true</p>
<h2 id="&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x548C;&#x62C6;&#x7BB1;&#x7684;&#x6982;&#x5FF5;">&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x548C;&#x62C6;&#x7BB1;&#x7684;&#x6982;&#x5FF5;</h2>
<p>Java &#x4E2D;&#x5F15;&#x5165;&#x4E86;&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x9488;&#x5BF9;&#x7740;&#x96C6;&#x4E2D;&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x53C8;&#x5F15;&#x5165;&#x4E86;&#x76F8;&#x5BF9;&#x5E94;&#x7684;&#x5305;&#x88C5;&#x7C7B;&#x578B;&#x3002;
Java &#x4E3A;&#x6BCF;&#x4E2A;&#x539F;&#x59CB;&#x7C7B;&#x578B;&#x63D0;&#x4F9B;&#x4E86;&#x5305;&#x88C5;&#x7C7B;&#x578B;&#xFF1A;</p>
<ul>
<li>&#x539F;&#x59CB;&#x7C7B;&#x578B;: boolean&#xFF0C;char&#xFF0C;byte&#xFF0C;short&#xFF0C;int&#xFF0C;long&#xFF0C;float&#xFF0C;double</li>
<li>&#x5305;&#x88C5;&#x7C7B;&#x578B;&#xFF1A;Boolean&#xFF0C;Character&#xFF0C;Byte&#xFF0C;Short&#xFF0C;Integer&#xFF0C;Long&#xFF0C;Float&#xFF0C;Double</li>
</ul>
<p>&#x4ECE;Java 5&#x5F00;&#x59CB;&#x5F15;&#x5165;&#x4E86;&#x81EA;&#x52A8;&#x88C5;&#x7BB1;/&#x62C6;&#x7BB1;&#x673A;&#x5236;&#xFF0C;&#x4F7F;&#x5F97;&#x539F;&#x59CB;&#x7C7B;&#x578B;&#x548C;&#x5305;&#x88C5;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x76F8;&#x4E92;&#x8F6C;&#x6362;&#x3002;
&#x524D;&#x9762;&#x4EE3;&#x7801;&#x4E2D;&#x5C31;&#x7528;&#x5230;&#x4E86;&#x81EA;&#x52A8;&#x88C5;&#x7BB1;/&#x62C6;&#x7BB1;&#x673A;&#x5236;&#xFF1A;</p>
<pre><code>Integer f1 = 127, f2 = 127, f3 = 128, f4 = 128; // &#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x6210;Integer&#x7C7B;&#x578B;
int i1 = f1, i2 = f2, i3 = f3, i4 = f4;//&#x81EA;&#x52A8;&#x62C6;&#x7BB1;&#x6210;int&#x7C7B;&#x578B;
</code></pre><p>&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x6765;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x5F15;&#x7528;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;jvm &#x5B9E;&#x9645;&#x4E0A;&#x8C03;&#x7528;&#x4E86; <code>valueOf()</code> &#x65B9;&#x6CD5;</p>
<h2 id="&#x95EE;&#x9898;&#x89E3;&#x6790;">&#x95EE;&#x9898;&#x89E3;&#x6790;</h2>
<p>&#x56E0;&#x4E3A; == &#x6BD4;&#x8F83;&#x7684;&#x662F;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x76F8;&#x540C;&#xFF0C;&#x660E;&#x663E; f1&#x548C;f2 &#x6307;&#x5411;&#x7684;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;f3 &#x548C; f4 &#x6307;&#x5411;&#x7684;&#x4E0D;&#x662F;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x3002;
&#x4E3A;&#x4EC0;&#x4E48;&#x4F1A;&#x8FD9;&#x6837;&#x5462;&#xFF1F;
&#x8FD9;&#x5C31;&#x5F52;&#x7ED3;&#x4E8E; Java &#x5BF9;&#x4E8E; Integer &#x4E0E; int &#x7684;&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x4E0E;&#x62C6;&#x7BB1;&#x7684;&#x8BBE;&#x8BA1;&#xFF0C;&#x8FD0;&#x7528;&#x4E86;&#x4E00;&#x79CD;&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;&#xFF1A;&#x4EAB;&#x5143;&#x6A21;&#x5F0F;&#x3002;
&#x4E3A;&#x4E86;&#x52A0;&#x5927;&#x5BF9;&#x7B80;&#x5355;&#x6570;&#x5B57;&#x7684;&#x91CD;&#x5229;&#x7528;&#xFF0C;java&#x5B9A;&#x4E49;&#xFF1A;&#x5728;&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x65F6;&#x5BF9;&#x4E8E;&#x503C;&#x4ECE;&#x2013;128&#x5230;127&#x4E4B;&#x95F4;&#x7684;&#x503C;&#xFF0C;&#x5B83;&#x4EEC;&#x88AB;&#x88C5;&#x7BB1;&#x4E3A;Integer&#x5BF9;&#x8C61;&#x540E;&#xFF0C;&#x4F1A;&#x5B58;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x88AB;&#x91CD;&#x7528;&#xFF0C;&#x59CB;&#x7EC8;&#x53EA;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x3002;
&#x770B;&#x4E00;&#x4E0B; <code>Integer.valueOf</code> &#x7684;&#x4EE3;&#x7801;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code>    public static Integer valueOf(int i) {
        if (i &gt;= IntegerCache.low &amp;&amp; i &lt;= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
</code></pre><p>&#x5728; <code>IntegerCache.low</code> &#x548C; <code>IntegerCache.high</code> &#x4E4B;&#x95F4;&#x7684;&#x6570;&#x4F1A;&#x76F4;&#x63A5;&#x5411;&#x7F13;&#x5B58;&#x6C60; <code>IntegerCache</code> &#x4E2D;&#x83B7;&#x53D6;&#x7F13;&#x5B58;&#x5BF9;&#x8C61;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x8FD9;&#x4E2A;&#x8303;&#x56F4;&#x4E4B;&#x5916;&#x5219;&#x91CD;&#x65B0;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x3002;
&#x518D;&#x6765;&#x770B;&#x4E00;&#x4E0B;  <code>IntegerCache</code> &#x7684;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code>    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty(&quot;java.lang.Integer.IntegerCache.high&quot;);
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k &lt; cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high &gt;= 127;
        }

        private IntegerCache() {}
    }
</code></pre><p>&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x521B;&#x5EFA;&#x4E86;&#x4E2A; [-128, 127] &#x7684;&#x6570;&#x5B57;&#x7684;&#x7F13;&#x5B58;&#x6C60;&#x3002;
&#x90A3;&#x4E48;&#xFF0C;&#x4E3A;&#x4EC0;&#x4E48;&#x8981;&#x8FD9;&#x4E48;&#x8BBE;&#x8BA1;&#x5462;&#xFF1F;&#x4E00;&#x822C;&#x6765;&#x8BF4;&#xFF0C;&#x5C0F;&#x6570;&#x5B57;&#x7684;&#x4F7F;&#x7528;&#x9891;&#x7387;&#x5F88;&#x9AD8;&#xFF0C;&#x5C06;&#x5C0F;&#x6570;&#x5B57;&#x4FDD;&#x5B58;&#x8D77;&#x6765;&#xFF0C;&#x8BA9;&#x5176;&#x59CB;&#x7EC8;&#x4EC5;&#x6709;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x8282;&#x7EA6;&#x5185;&#x5B58;&#xFF0C;&#x63D0;&#x9AD8;&#x6548;&#x7387;&#x3002;
&#x800C;&#x5982;&#x679C;&#x8D85;&#x8FC7;&#x4E86;&#x4ECE; &#x2013;128 &#x5230; 127 &#x4E4B;&#x95F4;&#x7684;&#x503C;&#xFF0C;&#x88AB;&#x88C5;&#x7BB1;&#x540E;&#x7684; <code>Integer</code> &#x5BF9;&#x8C61;&#x5E76;&#x4E0D;&#x4F1A;&#x88AB;&#x91CD;&#x7528;&#xFF0C;&#x5373;&#x76F8;&#x5F53;&#x4E8E;&#x6BCF;&#x6B21;&#x88C5;&#x7BB1;&#x65F6;&#x90FD;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A; <code>Integer</code> &#x5BF9;&#x8C61;&#x3002;&#x8FD9;&#x5C31;&#x662F;&#x4E3A;&#x4EC0;&#x4E48; f3 == f4 &#x4E3A;false&#x7684;&#x539F;&#x56E0;&#x4E86;&#x3002;
&#x4EE5;&#x4E0A;&#x7684;&#x73B0;&#x8C61;&#x662F;&#x7531;&#x4E8E;&#x4F7F;&#x7528;&#x4E86;&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x6240;&#x5F15;&#x8D77;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x6CA1;&#x6709;&#x4F7F;&#x7528;&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#xFF0C;&#x800C;&#x662F;&#x8DDF;&#x4E00;&#x822C;&#x7C7B;&#x4E00;&#x6837;&#xFF0C;&#x7528; new &#x6765;&#x8FDB;&#x884C;&#x5B9E;&#x4F8B;&#x5316;&#xFF0C;&#x5C31;&#x4F1A;&#x6BCF;&#x6B21; new &#x5C31;&#x90FD;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x6240;&#x4EE5; f5 == f6 &#x4E3A;false&#xFF1B;
&#x81F3;&#x4E8E; i1 == i2 &#x548C;i3 == i4&#xFF0C;&#x8FD9;&#x5C5E;&#x4E8E;&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x7684;&#x6BD4;&#x8F83;&#x3002;
i5 == f6 &#xFF1A;&#x5F53;int&#x548C;Integer&#x8FDB;&#x884C; == &#x6BD4;&#x8F83;&#x7684;&#x65F6;&#x5019;&#xFF0C;Java&#x4F1A;&#x628A;Integer&#x8FDB;&#x884C;&#x81EA;&#x52A8;&#x62C6;&#x7BB1;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x628A;Integer&#x8F6C;&#x6210;int&#x7C7B;&#x578B;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x91CC;&#x8FDB;&#x884C;&#x6BD4;&#x8F83;&#x7684;&#x662F;int&#x7C7B;&#x578B;&#x7684;&#x503C;&#xFF0C;&#x6240;&#x4EE5;&#x7ED3;&#x679C;&#x5373;&#x4E3A;true&#x3002;</p>
<h2 id="string-&#x7C7B;&#x7684;&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x62C6;&#x7BB1;">String &#x7C7B;&#x7684;&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x62C6;&#x7BB1;</h2>
<pre><code>        String s1 = &quot;str&quot;, s2 = &quot;str&quot;;
        String s3 = new String(&quot;str&quot;);
        String s4 = new String(&quot;str&quot;);
        System.out.println(s1 == s2);
        System.out.println(s3 == s4);
</code></pre><p>&#x7ED3;&#x679C;&#x4E3A; true false&#x3002;</p>
<p>String &#x6709;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;&#x6C60; - String Pool&#xFF0C;&#x901A;&#x8FC7;&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x5F97;&#x5230;&#x7684;&#x5BF9;&#x8C61;&#x90FD;&#x5B58;&#x50A8;&#x5728;&#x8FD9;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x5E38;&#x91CF;&#x6C60;&#x91CC;&#x9762;&#xFF0C;&#x76F8;&#x540C;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x4E0D;&#x4F1A;&#x91CD;&#x590D;&#x521B;&#x5EFA;&#x3002;
&#x901A;&#x8FC7;&#x663E;&#x5F0F; new &#x51FA;&#x6765;&#x7684;&#x5BF9;&#x8C61;&#x4E0D;&#x5728;&#x8FD9;&#x91CC;&#x9762;&#xFF0C;&#x6BCF;&#x6B21;&#x90FD;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5BF9;&#x8C61;&#x3002;</p>
<h2 id="long-&#x7C7B;&#x7684;&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x62C6;&#x7BB1;">Long &#x7C7B;&#x7684;&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x62C6;&#x7BB1;</h2>
<p>Long &#x7684;&#x81EA;&#x52A8;&#x88C5;&#x7BB1;&#x548C; Integer &#x7C7B;&#x4F3C;&#xFF1A;</p>
<pre><code>    public static Long valueOf(long l) {
        final int offset = 128;
        if (l &gt;= -128 &amp;&amp; l &lt;= 127) { // will cache
            return LongCache.cache[(int)l + offset];
        }
        return new Long(l);
    }
</code></pre><pre><code>    private static class LongCache {
        private LongCache(){}

        static final Long cache[] = new Long[-(-128) + 127 + 1];

        static {
            for(int i = 0; i &lt; cache.length; i++)
                cache[i] = new Long(i - 128);
        }
    }
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="java-basic-knowledge-generic.html" class="navigation navigation-prev " aria-label="Previous page: Java 基础 -- 范型">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="java-basic-knowledge-parameter-passing.html" class="navigation navigation-next " aria-label="Next page: Java 基础 -- 值传递、引用传递和参数传递">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Java 基础 -- 关于自动装箱和拆箱机制","categories":"Java","comments":true,"tags":["Java"],"description":"介绍 Java 的自动装箱和拆箱机制","date":"2014-02-12T10:00:00.000Z","level":"1.1.16","depth":2,"next":{"title":"Java 基础 -- 值传递、引用传递和参数传递","level":"1.1.17","depth":2,"path":"source/Java/java-basic-knowledge-parameter-passing.md","ref":"source/Java/java-basic-knowledge-parameter-passing.md","articles":[]},"previous":{"title":"Java 基础 -- 范型","level":"1.1.15","depth":2,"path":"source/Java/java-basic-knowledge-generic.md","ref":"source/Java/java-basic-knowledge-generic.md","articles":[]},"dir":"ltr"},"config":{"plugins":["expandable-chapters","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"expandable-chapters":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"寒江蓑笠的博客","gitbook":"*"},"file":{"path":"source/Java/java-basic-knowledge-auto-boxing.md","mtime":"2022-05-17T10:49:59.385Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-05-17T10:54:58.084Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

